<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class myad extends check{
	//广告列表
	public function lis(){
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->myad = spClass('m_myad')->spPager($page,15)->findAll(null,'odnum ASC,id ASC');
		$this->pager = spClass('m_myad')->spPager()->getPager();
		$this->display('myad_lis.tpl.php');
	}
	//添加广告
	public function add(){
		$this->display('myad_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_myad')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('myad','add'));
		if(false != spClass('m_myad')->create($this->spArgs())){
			$this->success('添加广告成功！',spUrl('myad','lis'));
		}else{
			$this->error('添加广告失败！',spUrl('myad','add'));
		}
	}
	//更新广告
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('myad','lis'));
		$this->myad = spClass('m_myad')->find(array('id'=>$id));
		$this->display('myad_upd.tpl.php');
	}
	public function doupd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('myad','lis'));
		if(false != spClass('m_myad')->update(array('id'=>$id),$this->spArgs())){
			$this->success('更新广告成功！',spUrl('myad','lis'));
		}else{
			$this->error('更新广告失败！',spUrl('myad','lis'));
		}
	}
	//删除广告
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('myad','lis'));
		$thumb = spClass('m_myad')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb'])){
			if(strpos($thumb['thumb'], 'thumb_') !== false){
				$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			}else{
				$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
			}
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_myad')->delete(array('id'=>$id))){
			$this->success('删除广告成功！',spUrl('myad','lis'));
		}else{
			$this->error('删除广告失败！',spUrl('myad','lis'));
		}
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_myad')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('myad','lis'));
	}
	//批量删除
	public function pldel(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要删除的编号！',spUrl('myad','lis'));
		for($i=0;$i<count($idrow);$i++){
			$thumb = spClass('m_myad')->find(array('id'=>$idrow[$i]),null,'thumb');
			if(!empty($thumb['thumb'])){
				if(strpos($thumb['thumb'], 'thumb_') !== false){
					$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
				}else{
					$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
				}
				if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
				if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
				spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
			}
			spClass('m_myad')->delete(array('id'=>$idrow[$i]));
		}
		$this->success('批量删除广告成功！',spUrl('myad','lis'));
	}
}